use image::{self, GenericImageView, ImageBuffer, Pixel, Rgb};

fn main() {
    let mut output_x = 0u32;
    let mut output_y = 0u32;

    let data_path = "./pics/".to_string();
    let start = 0u32;
    let end = 20u32;
    let endwith = ".png".to_string();

    // 收集信息，用于新建缓冲区
    for index in start..=end {
        let file_path = format!("{data_path}{index}{endwith}");
        let img = image::open(&file_path);
        if let Ok(img_info) = img {
            print!("打开{file_path}成功\t");
            let img_x = img_info.dimensions().0;
            let img_y = img_info.dimensions().1;
            print!("x:{img_x}, y:{img_y}\n");
            output_y += img_y;
            if img_x > output_x {
                output_x = img_x;
            }
        }
    }
    print!("output_x : {output_x}, output y : {output_y}\n");
    let mut imgbuf : ImageBuffer<Rgb<u8>, Vec<u8>> = ImageBuffer::new(output_x, output_y);

    // 读取并复制
    let mut pic_root_y = 0u32;
    for index in start..=end {
        let file_path = format!("{data_path}{index}{endwith}");
        let img = image::open(&file_path);
        if let Ok(img_info) = img {
            print!("打开{file_path}成功\t");
            let img_x = img_info.dimensions().0;
            let img_y = img_info.dimensions().1;
            for y in 0..img_y {
                for x in 0..img_x {
                    let pix = img_info.get_pixel(x, y).to_rgb();
                    let target_y = pic_root_y + y;
                    let target_x = x;
                    imgbuf.put_pixel(target_x, target_y, pix);
                }
            }
            print!("第{index}图片写入完成\n");
            pic_root_y += img_y;
        }
    }
    let save_path = format!("./output/mix.png");
    imgbuf.save(save_path);
    print!("图片连接完成!\n");

}
